package com.bicon.botu.finance.wallet.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.bicon.botu.finance.wallet.common.WalletConstants;
import com.bicon.botu.finance.wallet.entity.Wallet;
import com.bicon.botu.finance.wallet.mapper.FinancialAccountMapper;
import com.bicon.botu.core.base.common.Constants;
import com.bicon.botu.core.base.common.entity.BaseResult;
import com.bicon.botu.core.base.common.utils.StringUtils;
import com.bicon.botu.finance.wallet.entity.FinancialAccount;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户支付宝，微信，银行卡增删改查
 * @Author: sx
 * @Date: Created in 17:59 2017/6/21/021
 */
@Service
public class FinancialAccountService extends ServiceImpl<FinancialAccountMapper, FinancialAccount> {

    /***
     * 用户支付宝，微信，银行卡增加
     * @param userId
     * @param owner
     * @param financialNum
     * @param financialType
     * @return
     */
    public BaseResult add(String userId, String owner, String financialNum, String financialType, String phone, String identityCard) {
        //根据用户id查找账户
        Wallet wallet = new Wallet().selectOne(new EntityWrapper<Wallet>().eq("user_id", userId));
        if (null == wallet) {
            return new BaseResult(Constants.RESPONSE_CODE_401, Constants.RESPONSE_MESSAGE_401);
        }
        //创建金融卡片管理对象
        FinancialAccount entity = new FinancialAccount().selectOne(new EntityWrapper().eq("financial_num",financialNum)
        .eq("financial_type",financialType).eq("logic_delete", WalletConstants.DEL_LOGIC_NORMAL));
        if (null!=entity){
            return new BaseResult(Constants.RESPONSE_CODE_401, Constants.RESPONSE_MESSAGE_401);
        }
        FinancialAccount financialAccount = new FinancialAccount();
        financialAccount.setUserId(userId);
        financialAccount.setWalletId(wallet.getId());
        financialAccount.setOwner(owner);
        financialAccount.setFinancialNum(financialNum);
        financialAccount.setFinancialType(financialType);
        if (StringUtils.isNotBlank(phone)){
            financialAccount.setPhone(phone);
        }
        if (StringUtils.isNotBlank(identityCard)){
            financialAccount.setIdentityCard(identityCard);
        }
        financialAccount.preInsert();
        boolean result = financialAccount.insert();
        if (result) {
            return new BaseResult(Constants.RESPONSE_CODE_200, Constants.RESPONSE_MESSAGE_200);
        } else {
            return new BaseResult(Constants.RESPONSE_CODE_500, Constants.RESPONSE_MESSAGE_500);
        }
    }
    /**
     * 用户支付宝，微信，银行卡 删除
     * @param id
     * @return
     */
    public BaseResult delete(String id) {
        FinancialAccount financialAccount = new FinancialAccount().selectById(id);
        if (null == financialAccount) {
            return new BaseResult(Constants.RESPONSE_CODE_500, Constants.RESPONSE_MESSAGE_500);
        }
        financialAccount.setLogicDelete(WalletConstants.DEL_LOGIC_DELETE);
        financialAccount.preUpdate();
        boolean result = financialAccount.updateById();
        if (result) {
            return new BaseResult(Constants.RESPONSE_CODE_200, Constants.RESPONSE_MESSAGE_200);
        } else {
            return new BaseResult(Constants.RESPONSE_CODE_500, Constants.RESPONSE_MESSAGE_500);
        }
    }


    /**
     * 用户支付宝，微信，银行卡 修改
     * @param financialAccount
     * @return
     */
    public BaseResult update(FinancialAccount financialAccount) {
        //创建金融卡片管理对象
        FinancialAccount entity = new FinancialAccount().selectOne(new EntityWrapper().eq("financial_num",financialAccount.getFinancialNum())
                .eq("financial_type",financialAccount.getFinancialType()).eq("logic_delete",WalletConstants.DEL_LOGIC_NORMAL));
        if (null!=entity){
            return new BaseResult(Constants.RESPONSE_CODE_401, Constants.RESPONSE_MESSAGE_401);
        }
        boolean result = financialAccount.updateById();
        if (result) {
            return new BaseResult(Constants.RESPONSE_CODE_200, Constants.RESPONSE_MESSAGE_200);
        } else {
            return new BaseResult(Constants.RESPONSE_CODE_500, Constants.RESPONSE_MESSAGE_500);
        }
    }

    /**
     * 用户支付宝，微信，银行卡 查寻
     * @param userId
     * @param financialType
     * @return
     */
    public BaseResult selectList(String userId, String financialType) {
        EntityWrapper<FinancialAccount> entityWrapper = new EntityWrapper<FinancialAccount>();
        entityWrapper.eq("user_id", userId);
        if (StringUtils.isNotBlank(financialType)) {
            entityWrapper.eq("financial_type", financialType);
        }
        entityWrapper.eq("logic_delete", WalletConstants.DEL_LOGIC_NORMAL);
        List<FinancialAccount> financialAccountList = new FinancialAccount().selectList(entityWrapper);
        return new BaseResult(Constants.RESPONSE_CODE_200, Constants.RESPONSE_MESSAGE_200, financialAccountList);
    }
    /**
     * 用户支付宝，微信，银行卡 查寻
     * @param userId
     * @param id
     * @return
     */
    public FinancialAccount selectByIdAndUserid(String userId, String id) {
        EntityWrapper<FinancialAccount> entityWrapper = new EntityWrapper<FinancialAccount>();
        entityWrapper.eq("user_id", userId);
        entityWrapper.eq("logic_delete", WalletConstants.DEL_LOGIC_NORMAL);
        entityWrapper.eq("logic_delete", WalletConstants.DEL_LOGIC_NORMAL);
        FinancialAccount financialAccount = new FinancialAccount().selectOne(entityWrapper);
        return financialAccount;
    }
}
